# coding=utf-8
"""
    @project: 15python_spider
    @Author：frank
    @file： baidu_tieba.py
    @date：2023/12/19 21:18
"""
import time
import random
import requests
from fake_useragent import UserAgent


class BaiduSpider(object):
    def __init__(self):
        self.url = 'http://tieba.baidu.com/f'

    # 获取响应
    def get_page(self, url, kw):
        res = requests.get(url=url, params=kw, headers={'User-Agent': UserAgent().random})
        html = res.content.decode('utf-8', 'ignore')
        return html

    # 提取数据
    def parse_page(self):
        pass

    # 保存数据
    def write_page(self, filename, html):
        with open(filename, 'w', encoding='utf-8') as f:
            f.write(html)

    # 主函数
    def main(self):
        name = input('请输入贴吧名:')
        start = int(input('请输入起始页:'))
        end = int(input('请输入终止页:'))

        # 拼接URL地址，发请求
        for page in range(start, end + 1):
            pn = (page - 1) * 50
            params = {
                'kw': name,
                'pn': pn
            }
            # 获取响应，并保存
            html = self.get_page(self.url, params)
            filename = '{}-第{}页.html'.format(name, page)
            self.write_page(filename, html)
            print('第{}页爬取成功'.format(page))
            # 控制爬取速度
            time.sleep(random.randint(1, 3))


if __name__ == '__main__':
    spider = BaiduSpider()
    spider.main()
